package aole.mindfx;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import aole.mindfx.model.MindFXAttribute;
import aole.mindfx.model.MindFXClass;
import aole.mindfx.model.MindFXMethod;
import aole.mindfx.profile.Profiler;

public class EditClassPane extends GridPane {

	private Label				lblClass;
	private MindFXClass			fxclass;
	private TextField			txtHeader;
	private TextField			txtSource;
	private ComboBox< String >	attribTypes;
	private ComboBox< String >	methodTypes;
	private TextField			txtAttrib;
	private TextField			txtMethod;
	private FXNodeListener		listener;

	class HeaderEventHandler implements EventHandler< KeyEvent > {
		@Override
		public void handle( KeyEvent event ) {
			fxclass.setHeaderFile( txtHeader.getText() );
		}
	}

	class SourceEventHandler implements EventHandler< KeyEvent > {
		@Override
		public void handle( KeyEvent event ) {
			fxclass.setSourceFile( txtSource.getText() );
		}
	}

	class AttributeEventHandler implements EventHandler< ActionEvent > {
		@Override
		public void handle( ActionEvent event ) {
			MindFXAttribute a = new MindFXAttribute( txtAttrib.getText(),
					listener, fxclass );
			a.setType( attribTypes.getValue() );
			fxclass.addFXAttribute( a );
			txtAttrib.setText( "" );
			listener.actionPerformed( a );
		}
	}

	class MethodEventHandler implements EventHandler< ActionEvent > {
		@Override
		public void handle( ActionEvent event ) {
			MindFXMethod m = new MindFXMethod( txtMethod.getText(), listener,
					fxclass );
			m.setType( methodTypes.getValue() );
			fxclass.addFXMethod( m );
			txtMethod.setText( "" );
			listener.actionPerformed( m );
		}
	}

	class ShowCodeEventHandler implements EventHandler< ActionEvent > {
		@Override
		public void handle( ActionEvent event ) {
			listener.showCode( fxclass );
		}
	}

	class DeleteEventHandler implements EventHandler< ActionEvent > {
		@Override
		public void handle( ActionEvent event ) {
			listener.onDelete( fxclass );
		}
	}

	public EditClassPane( FXNodeListener l, Profiler profiler ) {
		super();
		listener = l;
		setPadding( new Insets( 12, 12, 12, 12 ) );
		setHgap( 5 );
		setVgap( 5 );
		int row = 1;

		// class label
		lblClass = new Label( "class ClassName" );
		lblClass.setStyle( MindFXClass.getStyle() );
		lblClass.setFont( MindFXClass.getFont() );
		add( lblClass, 1, row, 2, 1 );
		row++;

		// delete button
		Button b = new Button( "Delete" );
		b.setOnAction( new DeleteEventHandler() );
		add( b, 2, row, 1, 1 );
		row++;

		// header file
		add( new Label( "header" ), 1, row, 1, 1 );
		txtHeader = new TextField();
		txtHeader.setOnKeyPressed( new HeaderEventHandler() );
		add( txtHeader, 2, row, 1, 1 );
		row++;

		// source file
		add( new Label( "source" ), 1, row, 1, 1 );
		txtSource = new TextField();
		add( txtSource, 2, row, 1, 1 );
		txtSource.setOnKeyPressed( new SourceEventHandler() );
		row++;

		// add attribute
		attribTypes = new ComboBox< String >();
		attribTypes.setItems( profiler.getAttributeTypes() );
		attribTypes.setValue( "int" );
		add( attribTypes, 1, row, 1, 1 );
		txtAttrib = new TextField();
		add( txtAttrib, 2, row, 1, 1 );
		row++;

		Button ba = new Button( "Add Attribute" );
		ba.setOnAction( new AttributeEventHandler() );
		add( ba, 1, row, 2, 1 );
		row++;

		// add method
		methodTypes = new ComboBox< String >();
		methodTypes.setItems( profiler.getMethodTypes() );
		methodTypes.setValue( "void" );
		add( methodTypes, 1, row, 1, 1 );
		txtMethod = new TextField();
		add( txtMethod, 2, row, 1, 1 );
		row++;

		Button bm = new Button( "Add Method" );
		bm.setOnAction( new MethodEventHandler() );
		add( bm, 1, row, 2, 1 );
		row++;

		// show code
		bm = new Button( "Show Class Code" );
		bm.setOnAction( new ShowCodeEventHandler() );
		add( bm, 1, row, 2, 1 );
		row++;
	}

	public void setClass( MindFXClass c ) {
		fxclass = c;
		lblClass.setText( "class " + c.getName() );
		txtHeader.setText( c.getHeaderFile() );
		txtSource.setText( c.getSourceFile() );
	}

}
